import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

class SpUtil {
  /// set
  static setString(String key, String value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setString(key, value);
  }

  static setInt(String key, int value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setInt(key, value);
  }

  static setDouble(String key, double value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setDouble(key, value);
  }

  static setBool(String key, bool value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setBool(key, value);
  }

  static setMap(String key, Map<dynamic, dynamic> value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setString(key, jsonEncode(value));
  }

  static setList(String key, List<dynamic> value) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.setString(key, jsonEncode(value));
  }

  /// get
  static Future<String> getString(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.get(key);
  }

  static Future<int> getInt(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getInt(key);
  }

  static Future<double> getDouble(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getDouble(key);
  }

  static Future<bool> getBool(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getBool(key);
  }

  static Future<Map<dynamic, dynamic>> getMap(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    String valueS = sp.getString(key);
    return valueS == null ? valueS : jsonDecode(valueS);
  }

  static Future<List<dynamic>> getList(String key) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    String valueS = sp.getString(key);
    return valueS == null ? valueS : jsonDecode(valueS);
  }
}
